//
//  ZMCityseachTableViewController.m
//  DaMei
//
//  Created by mac on 16/2/23.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ZMCityseachTableViewController.h"
#import "ZMCitysModel.h"

@interface ZMCityseachTableViewController ()
//总的城市数据
@property (nonatomic, strong) NSArray *cityArray;

//搜索结果数据
@property (nonatomic, strong) NSMutableArray *resultArray;

@end

@implementation ZMCityseachTableViewController
- (NSArray *)cityArray {

    if (_cityArray == nil) {
        _cityArray = [ZMCitysModel objectArrayWithFilename:@"cities.plist"];
    }
    return _cityArray;
}

- (NSMutableArray *)resultArray {

    if (_resultArray == nil) {
        _resultArray = [NSMutableArray array];
    }
    return _resultArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
#pragma mark 实现搜索文字的set方法 --> 实时搜索, 只要属性发生了改变, 立即就会调用此方法
- (void)setSearchText:(NSString *)searchText {

    _searchText = [searchText copy];
    
    _searchText = searchText.lowercaseString;
    
    [self.resultArray removeAllObjects];
    
    for (ZMCitysModel *cityModel in self.cityArray) {
        if ([cityModel.name containsString:_searchText] || [cityModel.pinYin containsString:_searchText] || [cityModel.pinYinHead containsString:_searchText]) {
            
            [self.resultArray addObject:cityModel.name];
        }
    }
    
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.resultArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *ID = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    cell.textLabel.text = self.resultArray[indexPath.row];
    // Configure the cell...
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    return [NSString stringWithFormat:@"共有%zd个结果",self.resultArray.count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    通知发送选中的城市
    [ZMNotificationCenter postNotificationName:ZMCityDidChangeNotifacation object:nil userInfo:@{ZMSelectCityName : self.resultArray[indexPath.row]}];
//
    [self dismissViewControllerAnimated:YES completion:nil];

}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
